package com.yjs.app.trade.service.service.sys.impl;

import java.sql.Timestamp;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.yjs.app.trade.core.base.mybatis.dbutil.DynamicSwitchDataSource;
import com.yjs.app.trade.core.util.StringUtil;
import com.yjs.app.trade.entity.base.util.Page;
import com.yjs.app.trade.entity.bean.PassportSearch;
import com.yjs.app.trade.service.mapper.sys.FeedBackMapper;
import com.yjs.app.trade.service.service.sys.FeedbackService;

@Service("feedbackService")
@DynamicSwitchDataSource(dataSource="app")
public class FeedbackServiceImpl implements FeedbackService {
	
	@Autowired
	private FeedBackMapper feedBackMapper;
	/**
	 * 用户反馈分页查询
	 */
	@Override
	public Page<?> findUserFeedbackPage(Page<PassportSearch> page, PassportSearch passportSearch) {
		passportSearch.setPage(page);
		List<Map<String, Object>> findUserFeedbackPage = feedBackMapper.findUserFeedbackPage(passportSearch);
		for (Map<String, Object> map : findUserFeedbackPage) {
			for (Map.Entry<String,Object> entry: map.entrySet()) {
				if(entry.getValue() instanceof Timestamp){
					SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");
					map.put(entry.getKey(), sdf.format(new Date(((Timestamp)entry.getValue()).getTime())));
				}
				if(entry.getKey().equals("type")){
					map.put(entry.getKey(), getFeedName(Integer.parseInt(entry.getValue().toString())));
				}
			}
		}
		page.setMaps(findUserFeedbackPage);
		return page;
	}

	@Override
	@Transactional
	public int saveUserFeedback(String passportId, String username, int type, int category, String url,
			String content, String mobile) {
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("id", StringUtil.getUUID());
		map.put("passportId", passportId);
		map.put("username", username);
		map.put("type", type);
		map.put("category", category);
		map.put("url", url);
		map.put("content", content);
		map.put("mobile", mobile);
		map.put("createDt", new Date());
		return feedBackMapper.saveUserFeedback(map);
	}

	@Override
	public Map<String, Object> getUserFeedback(String faqId) {
		Map<String, Object> map = feedBackMapper.getUserFeedback(faqId);
		for (Map.Entry<String,Object> entry: map.entrySet()) {
			if(entry.getValue() instanceof Timestamp){
				SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");
				map.put(entry.getKey(), sdf.format(new Date(((Timestamp)entry.getValue()).getTime())));
			}
		}
		return map;
	}

	public String getFeedName (int type) {
		switch (type) {
		case 1:
			return "产品建议";
		case 2:
			return "业务建议";
		case 3:
			return "操作建议";
		case 4:
			return "其他";
		default:
			return "";
		}
	}
}
